<template>
  <page-header-wrapper :title="false">
    <a-card title="账户详情">
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="订单" :span="3"> {{ detail.id }}</a-descriptions-item>
        <a-descriptions-item label="创建人" :span="3"> {{ detail.creator }} </a-descriptions-item>
        <a-descriptions-item label="日期" :span="3"> {{ detail.createTime }} </a-descriptions-item>
        <a-descriptions-item label="金额" :span="3"> {{ detail.amount }} 元 </a-descriptions-item>
        <a-descriptions-item label="账户" :span="3">{{ detail.accName }}</a-descriptions-item>
        <a-descriptions-item label="状态" :span="3">{{ detail.type == 1 ? '已上账' : '未上账' }}</a-descriptions-item>
        <a-descriptions-item label="审核状态" :span="3">{{
          detail.checkStatus == 1 ? '已审核' : '未审核'
        }}</a-descriptions-item>
        <a-descriptions-item label="审核人" :span="3" v-if="detail?.checkStatus == 1"> {{ detail.checkerName }} </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3">{{ detail.memo }}</a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex; margin-top: 10px">
            <a-button 
              type="danger" 
              style="margin-right: 5px" 
              @click="handleEdit(detail)" 
              v-if="detail?.checkStatus == 0">编辑订单</a-button>
            <a-button 
              type="primary" 
              style="margin: 0px 5px" 
              @click="setCX()" 
              v-if="detail?.checkStatus == 0"
            >撤销订单</a-button
            >
            <a-button 
              type="success" 
              style="margin: 0px 5px" 
              v-if="detail?.checkStatus == 0" 
              @click="setSH()"
            >审核订单</a-button
            >
            <a-button 
              type="success" 
              style="margin: 0px 5px" 
              v-if="detail?.checkStatus == 1" 
              @click="setFSH()"
            >反审核</a-button
            >
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>
    <a-card title="操作记录" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" :loading="recordLoading" @click="handleRecordSearch(queryParam)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <!-- <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!-- <div class="table-operator">
        <a-button icon="cloud-upload">导出</a-button>
      </div> -->

      <a-table 
        ref="table" 
        size="default" 
        rowKey="key" 
        :columns="columns"
        :loading="recordLoading"
        :data-source="recordloadData"
        :pagination="recordPagination"
        @change="handleRecordTableChange">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template> {{ record.empName }} <a-tag color="#428bca" @click="handleUserDetail(record)">详情</a-tag> </template>
        </span>
      </a-table>
    </a-card>
    <group-form
      ref="createModal"
      :visible="visible"
      :loading="loading"
      :model="mdll"
      @cancel="handleCancel"
      @ok="handleOk"
    />
  </page-header-wrapper>
</template>

<script>
import GroupForm from './groupForm'
import { getSvAcclogDetailUsingGET, updateSvSuppliesUsingPOST,deleteSvAcclogUsingPOST } from '@/api/api/shouhouguanli.ts'
import { getOperRecordListUsingPOST } from '@/api/api/caigouguanli.ts'
export default {
  name: 'ServiceSupplierOrderDetail',
  components: {
    GroupForm,
  },
  data() {
    return {
      queryParam: {},
      advanced: false,
      mdll: null,
      mdl: null,
      visible: false,
      groupVisible: false,
      groupLoading: false,
      value: '',
      recordLoading:false,
      confirmLoading: false,
      columns: [
        {
          title: '时间',
          dataIndex: 'date',
        },
        {
          title: '类型',
          dataIndex: 'action',
        },
        {
          title: '操作',
          dataIndex: 'option',
          width: '150px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      detail: {},
      loadData: [],
    }
  },
  mounted() {
    this.handleSearch()
    this.handleRecordSearch(this.queryParam)
  },
  methods: {
    handleUserDetail(record) {
      this.$router.push({ path: `/user/user/${record.operator}` })
    },
    setCX() {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          deleteSvAcclogUsingPOST({ id: that.detail.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setSH() {
      const that = this
      that.$confirm({
        title: '审核订单',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          shareCheckPurUsingPOST({ id: that.detail.id })
            .then(() => {
              that.handleSearch()
              that.handleRecordSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setFSH() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          shareUnCheckPurUsingPOST({ id: that.detail.id })
            .then(() => {
              that.handleSearch()
              that.handleRecordSearch(that.queryParam)
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    handleSearch() {
      getSvAcclogDetailUsingGET({ id: this.$route.params.id })
        .then((res) => {
          this.detail = res.data
        })
        .catch((error) => {
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleEdit(detail) {
      this.mdll = { ...detail }
      this.visible = true
    },
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          // 修改 e.g.
          const params = {
            ...values,
            svSupply: this.$route.params.id,
            date: this.detail.date,
          }
          updateSvSuppliesUsingPOST({ ...params })
            .then(() => {
              this.handleSearch()
              this.handleRecordSearch(this.queryParam)
              this.$message.success('操作成功')
              this.visible = false
              this.confirmLoading = false
            })
            .catch((error) => {
              this.confirmLoading = false
              this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleRecordSearch(params) {
      this.recordLoading = true
      getOperRecordListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.recordloadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleRecordTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.recordPagination = pager
      this.handleRecordSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: '',
        status: '',
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
  },
}
</script>
